package com.loongtech.bi.manager.system;

import com.loongtech.bi.entity.POJO.Page;
import com.loongtech.bi.entity.system.EntityGameParamType;
import com.loongtech.core.jpa.manager.ManagerQueryCacheBase;
import com.loongtech.core.util.Utils;
import org.springframework.stereotype.Service;

import java.util.*;

import static com.loongtech.bi.manager.system.SysProjectManager.projectIdNameMap;

@Service
public class GameParamTypeConfigManager extends ManagerQueryCacheBase<EntityGameParamType> {
	private static final long serialVersionUID = 2L;

	public static int FIRST_ID = 1;  // 首个GameParamTypeID
	public static List<EntityGameParamType> GAME_PARAM_TYPE_LIST;  // EntityGameParamType全表
	public static Map<Integer, EntityGameParamType> ID2GAMEPARAMTYPE_MAP;  // id=EntityGameParamType
	public static Map<String, EntityGameParamType> NAME2GAMEPARAMTYPE_MAP;  // name=EntityGameParamType

	public static Map<Integer, Boolean> ID2OPEN_MAP;  // id=open
	public static Map<Integer, String> ID2NAME_MAP;  // id=name
	public static Map<String, Integer> gameParamTypeItmes;
	public static Map<String, String> gameParamTypeItmesRevise;

	public void init() {
		GAME_PARAM_TYPE_LIST = findByQuery("from EntityGameParamType order by id");
		if (GAME_PARAM_TYPE_LIST != null && GAME_PARAM_TYPE_LIST.size() > 0) {
			FIRST_ID = GAME_PARAM_TYPE_LIST.get(0).getId();
		} else {
			EntityGameParamType e = new EntityGameParamType();
			e.setName("常用游戏参数对应关系");
			e.setOpen(true);
			persist(e);

			GAME_PARAM_TYPE_LIST.add(e);
		}

		ID2GAMEPARAMTYPE_MAP = new HashMap<>();
		NAME2GAMEPARAMTYPE_MAP = new HashMap<>();
		ID2NAME_MAP = new LinkedHashMap<>();
		ID2OPEN_MAP = new LinkedHashMap<>();
		gameParamTypeItmes = new LinkedHashMap<>();
		gameParamTypeItmesRevise = new LinkedHashMap<>();
		for (EntityGameParamType e : GAME_PARAM_TYPE_LIST) {
			ID2GAMEPARAMTYPE_MAP.put(e.getId(), e);
			NAME2GAMEPARAMTYPE_MAP.put(e.getName(), e);
			ID2NAME_MAP.put(e.getId(), e.getName());
			ID2OPEN_MAP.put(e.getId(), e.isOpen());

			if (e.isOpen()) {
				gameParamTypeItmes.put(e.getName(), e.getId());
				gameParamTypeItmesRevise.put(e.getId() + "", e.getName());
			}
		}
	}

	/**
	 * 能否添加（id和name没重复）
	 * @param e
	 * @return
	 */
	public static boolean canAdd(EntityGameParamType e) {
		if (NAME2GAMEPARAMTYPE_MAP == null) {
			return true;
		}

		Boolean b = ID2OPEN_MAP.get(e.getId()) == null ? false : ID2OPEN_MAP.get(e.getId());
		return b.equals(e.isOpen()) ? !NAME2GAMEPARAMTYPE_MAP.containsKey(e.getName()) : true;
	}

    /**
     * 查询任务(springmvc)
     */
    public Page<List<EntityGameParamType>> queryByCriteria(Integer curPage, Integer pageSize, EntityGameParamType entity) {
        List<Object> cons = new ArrayList<>();
        StringBuilder hql = new StringBuilder(" from EntityGameParamType e where 1=1 ");
        if (!Utils.isEmptyStr(entity.getName())) {
            hql.append(" and e.name like ?");
            cons.add("%" + entity.getName() + "%");
        }
        if (entity.getGameId() != null) {
            hql.append(" and e.gameId like ?");
            cons.add("%" + entity.getGameId() + "%");
        }
        hql.append(" order by e.gameId,e.id");
        Integer totalSize = countNum(hql.toString(), cons.toArray());
        //数据
        List<EntityGameParamType> list = findByQuery((curPage - 1) * pageSize, pageSize, hql.toString(), cons.toArray());
        if (list != null && list.size() > 0) {
            for (EntityGameParamType paramtype : list) {
                String gameid = paramtype.getGameId();
                for (Map.Entry<String, String> entry : projectIdNameMap.entrySet()) {
                    gameid = gameid.replace(entry.getKey() + ",", entry.getValue() + ",");
                }
                paramtype.setGameName(gameid);
            }
        }
        return new Page<>(list, totalSize, curPage, pageSize);
    }

	public static String getNameById(int id) {
		return ID2NAME_MAP.get(id) == null ? id + "" : ID2NAME_MAP.get(id);
	}
}
